package com.fqxiny.lease.web.admin.custom.converter;

import com.fqxiny.lease.model.enums.BaseEnum;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.stereotype.Component;

/**
 * @author : 戚欣扬
 * @code : 接收参数枚举类型转换
 */
@Component
public class StringToBaseEnumConverterFactory implements ConverterFactory<String, BaseEnum> {
    @Override
    public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
        return string->{
            T[] enumConstants = targetType.getEnumConstants();
            for (T enumConstant : enumConstants) {
                if(enumConstant.getCode().equals(Integer.valueOf(string))){
                    return enumConstant;
                }
            }
            throw new IllegalArgumentException("参数非法");
        };
    }
}


